package com.evildog.workspace.dependencies.web.component;

import cn.hutool.json.JSONUtil;
import com.evildog.workspace.log.api.feign.AccessLogClient;
import com.evildog.workspace.log.api.feign.ExceptionLogClient;
import com.evildog.workspace.log.api.model.AccessLogParam;
import com.evildog.workspace.log.api.model.ExceptionLogParam;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.exception.ExceptionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;

@Component
@Slf4j
public class LogSaver {

    @Autowired
    private AccessLogClient accessLogClient;

    @Autowired
    private ExceptionLogClient exceptionLogClient;

    /**
     * 异步入库
     */
    @Async
    public void addAccessLog(AccessLogParam accessLog) {
        try {
            accessLogClient.save(accessLog);
        } catch (Throwable th) {
            log.error("[插入访问日志({}) 发生异常({})", JSONUtil.toJsonStr(accessLog), ExceptionUtils.getRootCauseMessage(th));
        }
    }

    @Async
    public void addExceptionLog(ExceptionLogParam exceptionLog) {
        try {
            exceptionLogClient.save(exceptionLog);
        } catch (Throwable th) {
            log.error("[插入异常日志({}) 发生异常({})", JSONUtil.toJsonStr(exceptionLog), th);
        }

    }
}
